# -*- coding: utf-8 -*-
# models
from openerp import fields, models, api, _

class Account(models.Model):
    _inherit = 'account.analytic.account'

    @api.one
    @api.depends('consignee')
    def _compute_transportation_price(self):        
        address_p = self.consignee.use_parent_address and self.consignee.parent_id or self.consignee
        self.transportation_price = self.env['transportation.pricelist'].get_address_price(
            # self.create_date,
            address_p.country_id,
            address_p.state_id,
            address_p.city,
            self.create_date
            )

    project=fields.Many2one('sale.project',string="Project",states={'managerconfirmed': [('readonly', True)]},domain=lambda self: [('applicant_id','in',[self.env.user.id])])
    predict_quantity=fields.Float(string="Predict Quantity(m³)")
    penalty=fields.Float(string="penalty",states={'managerconfirmed': [('readonly', True)]})
    date_remittance=fields.Date(string="Remittancetime",states={'managerconfirmed': [('readonly', True)]})
    litigation=fields.Char(string="litigation",states={'managerconfirmed': [('readonly', True)]})
    consignee = fields.Many2one('res.partner',string="Consignee",states={'managerconfirmed': [('readonly', True)]})
    bancai=fields.One2many('account.order.line','account_bc_id',string="预计板材明细",states={'managerconfirmed': [('readonly', True)]})
    qikuai=fields.One2many('account.order.line','account_qk_id',string="预计砌块明细")
    partner_id=fields.Many2one('res.partner',string=u"客户",domain=[('customer','=',True)],states={'managerconfirmed': [('readonly', True)]})
    manager_id=fields.Many2one('res.users',track_visibility='onchange',readonly=True,default=lambda self: self.env.user)
    fix_price_invoices=fields.Boolean(string="Fixed Price",default=True,readonly=True)
    recurring_invoices=fields.Boolean(string="Generate recurring invoices automatically",default=True,readonly=True)
    recurring_interval=fields.Integer(states={'managerconfirmed': [('readonly', True)]})
    recurring_rule_type=fields.Selection([
            ('daily', u'天'),
            ('weekly', u'周'),
            ('monthly',u'月'),
            ('yearly', u'年'),
            ],states={'managerconfirmed': [('readonly', True)]})
    recurring_next_date=fields.Date(states={'managerconfirmed': [('readonly', True)]})
    compare_user_manager_id=fields.Boolean(string=u"比较当前用户是否为合同提交用户",compute='_get_compare_user_manager_id')
    type_account = fields.Selection([
        ('account0',u'销售合同'),
        ('account1',u'代理合同'),
        ('account2',u'运输合同'),
        ('account3',u'零售合同')
        ])
   

    #运费合同所需要字段
    transportation_price = fields.Float(u'运费(元/吨)', compute='_compute_transportation_price',required=True)
    qikuai_m1=fields.Float(string=u"B05级砌块运费换算率(kg/m³)")
    qikuai_m2=fields.Float(string=u"B06级砌块运费换算率(kg/m³)")
    bancai_m=fields.Float(string=u"板材运费换算率(kg/m³)")
    b_m=fields.Float(string=u"等外品运费换算率(kg/m³)")
    mutuo_m=fields.Float(string=u"木托补贴费(元/m³)")
    incar_m=fields.Float(string=u"装车费(元/m³)")
    inbox_m=fields.Float(string=u"装箱费(元/m³)")
    outcar_m=fields.Float(string=u"卸货费(元/m³)")
    infuliao_m=fields.Float(string=u"辅料装货费(元/吨)")
    outfuliao_m=fields.Float(string=u"辅料卸货费(元/吨)")
    #收货地址
    street_id=fields.Char(string=u"街道", size=24)
    city_id = fields.Many2one('res.country.state.city', string=u'城市')
    state_id=fields.Many2one("res.country.state", string=u"省", ondelete='restrict')
    country_id=fields.Many2one('res.country', string=u"国家",ondelete='restrict',default=49)
    state=fields.Selection([
        ('template', 'Template'),
        ('draft','New'),
        ('open','In Progress'),
        ('pending','To Renew'),
        ('close','Closed'),
        ('new',u'草稿'),
        ('cancelled', 'Cancelled'),
        ('confirmed',u'市场组审核'),
        ('marketconfirmed',u'销售总监审核'),
        ('saleconfirmed',u'财务经理审核'),
        ('financialconfirmed',u'律师审核'),
        ('lawyerconfirmed',u'总经理审核'),
        ('managerconfirmed',u'合同通过')],default='new')

    _order = 'date_start desc, id desc'

    #权限所需要的字段
    user_manager=fields.Many2one('res.users',string=u"创建客户用户的部门经理",store=True)
    user_manager2=fields.Many2one('res.users',string=u"创建用户的上级部门经理",store=True)
    user_manager3 = fields.Many2one('res.users', string=u"当前用户的上级部门副经理", store=True)
    user_department = fields.Many2one('hr.department',string=u"代理商",domain=[('parent_id','=',False)],)
    menbers1_id = fields.Many2many(comodel_name='res.users',relation='account_analytic_account_res_users_rel_1',string=u"本级代理商账号",store=True)
    menbers2_id = fields.Many2many(comodel_name='res.users',relation='account_analytic_account_res_users_rel_2',string=u"本级代理商下级账号",store=True)

    @api.one
    def _get_manager_name(self):
        return self.user_manager.name

    @api.one
    def _get_manager2_name(self):
        return self.user_manager2.name

    @api.onchange('manager_id')
    def _onchange_create_uid(self):
        if self.manager_id and self.manager_id.employee_ids[0]:
            self.user_manager = self.manager_id.employee_ids[0].department_id.manager_id.user_id
            if self.manager_id.employee_ids[0].department_id.parent_id:
                self.user_manager2=self.manager_id.employee_ids[0].department_id.parent_id.manager_id.user_id
                self.user_manager3 = self.manager_id.employee_ids[0].department_id.parent_id.fumanager_id.user_id

    @api.onchange('user_department')
    def _onchange_create_user_department(self):
        if self.user_department:
            menbers = self.user_department.member_ids
            self.menbers1_id = self.env['res.users']
            for menber in menbers:
                self.menbers1_id|= menber.user_id
            if self.user_department.child_ids: 
                self.menbers2_id = self.env['res.users']
                for department in self.user_department.child_ids:
                    for menber in department.member_ids:
                         self.menbers2_id|=menber.user_id
           
    
    @api.one
    def action_new(self):
        self.write({'state':'new'})

    @api.one
    def action_confirm(self):
        self.write({'state':'confirmed'})

    @api.one
    def action_marketconfirm(self):
        self.write({'state':'marketconfirmed'})

    @api.one
    def action_saleconfirm(self):
        self.write({'state':'saleconfirmed'})

    @api.one
    def action_financialconfirm(self):
        self.write({'state':'financialconfirmed'})

    @api.one
    def action_lawyerconfirm(self):
        self.write({'state':'lawyerconfirmed'})

    @api.one
    def action_managerconfirm(self):
        self.write({'state':'managerconfirmed'})

    @api.one
    def action_cancel(self):
        self.write({'state':'new'})

    @api.onchange('project')
    def _change_project(self):
        if self.project:
            self.partner_id = self.project.customer_id

    @api.one
    @api.depends('manager_id')
    def _get_compare_user_manager_id(self):
        if self.manager_id:
            contract_manager_id=self.manager_id
            contract_user_id=self.env.user
            print(contract_manager_id,contract_user_id)
            print(contract_manager_id.id==contract_user_id.id)
            self.compare_user_manager_id =contract_manager_id.id==contract_user_id.id

class transport_account(models.Model):
    _name = 'transport.account'
    
    transport_company=fields.Many2one('res.partner',string="运输公司")
    qikuai_m1=fields.Float(string=u"B05级砌块运费换算率(kg/m³)")
    qikuai_m2=fields.Float(string=u"B06级砌块运费换算率(kg/m³)")
    bancai_m=fields.Float(string=u"板材运费换算率(kg/m³)")
    b_m=fields.Float(string=u"等外品运费换算率(kg/m³)")
    mutuo_m=fields.Float(string=u"木托补贴费(元/m³)")
    incar_m=fields.Float(string=u"装车费(元/m³)")
    inbox_m=fields.Float(string=u"装箱费(元/m³)")
    outcar_m=fields.Float(string=u"卸货费(元/m³)")
    infuliao_m=fields.Float(string=u"辅料装货费(元/m³)")
    outfuliao_m=fields.Float(string=u"辅料卸货费(元/m³)")